package com.car.loan.project.cm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.SwitchStatus;
import com.car.loan.common.enums.UserStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.DateUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.project.cm.domain.CmCar;
import com.car.loan.project.cm.mapper.CmCarMapper;
import com.car.loan.project.cm.service.ICmCarService;
import com.car.loan.project.cm.util.enunms.CarBrandType;
import com.car.loan.project.cm.util.select.CarDataSelect;
import com.car.loan.project.cm.util.vo.SelectedCarVo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 车型Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-19
 */
@Service
public class CmCarServiceImpl implements ICmCarService {

    @Autowired
    private CmCarMapper cmCarMapper;

    /**
     * 查询车型
     *
     * @param id 车型ID
     * @return 车型
     */
    @Override
    public CmCar selectCmCarById(Long id) {
        return cmCarMapper.selectById(id);
    }

    /**
     * 查询车型列表
     *
     * @param cmCar 车型
     * @return 车型
     */
    @Override
    public List<CmCar> selectCmCarList(CmCar cmCar) {
        QueryWrapper<CmCar> queryWrapper = new QueryWrapper<>(cmCar);
        return cmCarMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CmCar> selectPage(IPage<CmCar> page, QueryWrapper<CmCar> queryWrapper) {
        return cmCarMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<SelectedCarVo> selectedPage(CarDataSelect carDataSelect) {
        IPage<CmCar> page = new Page<>(carDataSelect.getPageNum(),
            carDataSelect.getPageSize());
        CmCar carQuery = new CmCar();
        carQuery.setType(carDataSelect.getType());
        if (null != carDataSelect.getParentId() && carDataSelect.getParentId() > 0) {
            carQuery.setParentId(carDataSelect.getParentId());
        }
        QueryWrapper<CmCar> queryWrapper = new QueryWrapper<>(carQuery);
        if (StringUtils.isNotEmpty(carDataSelect.getName())) {
            if (CarBrandType.BRAND.type == carDataSelect.getType()) {
                queryWrapper.like("brand", carDataSelect.getName());
            } else if (CarBrandType.SERIES.type == carDataSelect.getType()) {
                queryWrapper.like("series", carDataSelect.getName());
            } else if (CarBrandType.NAME.type == carDataSelect.getType()) {
                queryWrapper.like("name", carDataSelect.getName());
            }
        }
        if (null == carDataSelect.getOf() || SwitchStatus.OPEN.status == carDataSelect.getOf()) {
            queryWrapper.eq("status", UserStatus.OK.getCode());
        } else {
            queryWrapper.in("status", UserStatus.OK.getCode(), UserStatus.DISABLE.getCode());
        }
        queryWrapper.orderByDesc("id");
        IPage<CmCar> cmCarIPage = this.selectPage(page, queryWrapper);

        List<CmCar> records = cmCarIPage.getRecords();
        IPage<SelectedCarVo> result = new Page<>();
        BeanUtils.copyBeanProp(result, cmCarIPage);
        List<SelectedCarVo> selectedVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(data -> {
                SelectedCarVo selectedAccountVo = new SelectedCarVo();
                selectedAccountVo.setValue(String.valueOf(data.getId()));
                selectedAccountVo.setCarType(data.getType());
                selectedAccountVo.setStatus(data.getStatus());
                if (CarBrandType.BRAND.type == data.getType()) {
                    selectedAccountVo.setLabel(data.getBrand());
                } else if (CarBrandType.SERIES.type == data.getType()) {
                    selectedAccountVo.setLabel(data.getSeries());
                } else if (CarBrandType.NAME.type == data.getType()) {
                    selectedAccountVo.setLabel(data.getName());
                }
                selectedVos.add(selectedAccountVo);
            });
        }
        result.setRecords(selectedVos);
        return result;
    }

    /**
     * 新增车型
     *
     * @param cmCar 车型
     * @return 结果
     */
    @Override
    public int insertCmCar(CmCar cmCar) {
        cmCar.setCreateTime(DateUtils.getNowDate());
        return cmCarMapper.insert(cmCar);
    }

    /**
     * 修改车型
     *
     * @param cmCar 车型
     * @return 结果
     */
    @Override
    public int updateCmCar(CmCar cmCar) {
        return cmCarMapper.updateById(cmCar);
    }

    /**
     * 删除车型对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCmCarByIds(Collection<Long> ids) {
        return cmCarMapper.deleteBatchIds(ids);
    }

    /**
     * 删除车型信息
     *
     * @param id 车型ID
     * @return 结果
     */
    @Override
    public int deleteCmCarById(Long id) {
        return cmCarMapper.deleteById(id);
    }
}
